<?php
namespace app\api\controller;
use app\common\controller\ApiBase;
use think\facade\View;
use think\facade\Request;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Session;
use think\facade\Env;
use think\facade\Filesystem;
use think\facade\Config;
use app\common\model\Wechat;
use app\common\model\Uploads as Up;

class Upload extends ApiBase
{
    protected $noAuth = [ 'index'];
    public function initialize()
    {
        parent::initialize();
        $this->app = Wechat::wxapp();
    }
    //上传图片
    public function index()
{
        $file = request()->file('file');
        
        //小程序图片检测
        $result = $this->app->content_security->checkImage($file);
        if($result['errcode']=='87014'){
            $this->result('','-3','图片违规', 'json');
        }
        //处理图片
        Up::UploadValidate($file);
        $params = Config::load('setting/qiniu','params');
        //判断上传位置
        if($params['type']=='1'){//七牛
            $key = date('Y/md/His_').substr(microtime(), 2, 6).'_'.mt_rand(0,999).'.'.$file->getOriginalExtension();
            $qiniu = new \app\common\library\Qiniu();
            $url = $params['domain'].$qiniu->upload($file->getRealPath(), $key);
            //小程序图片检测
            $result = $app->content_security->checkImage($file->getOriginalName());
            if($result['errcode']=='87014'){
                $this->result('','0','图片违规', 'json');
            }
            Up::create([
                'model' => 'api',
                'type' => '1',
                'user_id'  => UserId(),
                'filename' => $file->getOriginalName(),
                'filesize' => $file->getSize(),
                'mine' => $file->getOriginalMime(),
                'url'      =>  $url
            ]);
            insert_api_log('上传了图片');
            $this->result($url,'0','上传成功', 'json');
        }else{//默认本地
            $savename = Filesystem::disk('public')->putFile('uploads',$file);
            $url = request()->domain().'/'.$savename;
            Up::create([
                'model' => 'api',
                'type' => '0',
                'user_id'  => UserId(),
                'filename' => $file->getOriginalName(),
                'filesize' => $file->getSize(),
                'mine' => $file->getOriginalMime(),
                'url'      =>  $url
            ]);
            insert_api_log('上传了图片');
            $this->result($url,'0','上传成功', 'json');
        }

        
    }
    //删除文件
    public function del($url)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $qiniuconfig = Config::load('setting/qiniu');
            $key = str_replace($qiniuconfig['domain'],'',$url);
            $qiniu = new \app\common\library\Qiniu();
            $qiniu->delete($key);
            Up::where(['url'=>$param['url']])->update(['delete_time'=>time()]);
            insert_api_log('删除了图片');
            $this->result($key, '0', '删除成功', 'json');
            
        }
    }
    
    

}
